package com.mz.mattress.client.move;

import com.mz.mattress.api.entity.*;
import com.mz.mattress.api.util.DateUtil;
import com.mz.mattress.api.util.IdUtil;
import com.mz.mattress.client.mapper.*;
import com.mz.mattress.client.service.OrderDataService;
import com.mz.mattress.client.service.OrderUseTimesService;
import com.mz.mattress.client.util.GenUtil;
import com.mz.mattress.client.util.RandomGenerator;
import com.mz.mattress.client.util.RandomBirthdayGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@Slf4j
@Service
public class MoveServiceImpl implements MoveService {

    @Autowired
    private TechMapper techMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberMerMapper memberMerMapper;

    @Autowired
    private OrderDataService orderDataService;

    @Autowired
    private OrderUseTimesService orderUseTimesService;

    @Override
    public void gen() {

//        String startDate = "2025-02-28";
//        String endDate = "2025-03-31";

//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
//        Date date1 = sdf1.parse(startDate);
//        Date date2 = sdf1.parse(endDate);

//        genMerchant();

//        genDevice();

//        genMember();

//        updateMember();

//        LocalDate startDate = LocalDate.of(2025, 7, 31);
//        for (int i = 0; i < 1; i++) {
//            log.info(startDate.toString());
//            orderDataService.genOrderDataByDay(DateUtil.toDateFromLocalDate(startDate));
//            startDate = startDate.plusDays(1);
//        }

        for (int i = 20704; i <= 22260; i++) {
            orderUseTimesService.sendBonus(i);
            System.out.println(i);
        }

//        registerMerchantMember();


//        orderDataService.genOrderDataByDay(DateUtil.toDate(startDate));

    }


    public void updateMember() {

        List<Long> longs = memberMapper.listId();

        List<String> list = RandomBirthdayGenerator.generateRandomBirthdays(longs.size());

        for (int i = 0; i < longs.size(); i++) {
            Member member = new Member();
            member.setId(longs.get(i));
            member.setSex(RandomGenerator.nextSex());
            member.setWeight(RandomGenerator.nextWeight());
            member.setBirthday(list.get(i));
            memberMapper.updateById(member);
        }

    }

    public void genMember() {

        String startDate = "2025-08-02";
        String endDate = "2025-08-03";
        Date start = DateUtil.toDate(startDate);
        Date end = DateUtil.toDate(endDate);

        Integer count = 190;

        List<String> sortedTimes = RandomGenerator.generateSortedRandomTimes(start, end, count);

        for (int i = 0; i < count; i++) {

            String time = sortedTimes.get(i);
            Member user = new Member();
            user.setId(createMemberId(time));
            user.setOpenId("");
            user.setSessionKey("");
            user.setMobile(GenUtil.generateMobileNumber());
            user.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132");
            user.setNickname("微信用户");
            user.setStatus(0);
            user.setCount(0);
            user.setCreateTime(time);
            user.setUpdateTime(time);
            memberMapper.insert(user);

        }

    }

    private static Long createMemberId(String time) {

        String ss = time.replace(" ", "").replace("-", "").replace(":", "");
        String str = ss.substring(2) + (10 + ThreadLocalRandom.current().nextInt(89));
        return Long.valueOf(str);

    }


    public void genDevice() {

        String startDate = "2025-02-28";
        String endDate = "2025-03-31";
        Date start = DateUtil.toDate(startDate);
        Date end = DateUtil.toDate(endDate);

        List<String> sortedTimes = RandomGenerator.generateSortedRandomTimes(start, end, 80);

        for (int i = 0; i < 80; i++) {

            Device device = new Device();
            device.setDeviceCode(IdUtil.uuid().substring(0, 24));
            device.setMerchantId(i + 1);
            device.setStatus(0);
            device.setProductKey("0104101421444080");
            device.setCreateTime(sortedTimes.get(i));
            deviceMapper.insert(device);

            Device device2 = new Device();
            device2.setDeviceCode(IdUtil.uuid().substring(0, 24));
            device2.setMerchantId(i + 1);
            device2.setStatus(0);
            device2.setProductKey("0104101421444080");
            device2.setCreateTime(sortedTimes.get(i));
            deviceMapper.insert(device2);

        }


    }

    private void insertDevice(Integer merchantId) {

        Device device = new Device();
        device.setMerchantId(merchantId);
        device.setCreateTime(DateUtil.getNowDate());


    }

    public void registerMerchantMember() {

        List<Merchant> list = merchantMapper.listAll();

        for (Merchant merchant : list) {

            MemberMer user = new MemberMer();
            user.setId(createMemberId(merchant.getCreateTime()));
            user.setOpenId("");
            user.setSessionKey("");
            user.setMobile(merchant.getMobile());
            user.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132");
            user.setNickname("微信用户");
            user.setStatus(0);
            user.setType(1);
            user.setTypeId(merchant.getId());
            user.setCreateTime(merchant.getCreateTime());
            user.setUpdateTime(merchant.getCreateTime());
            memberMerMapper.insert(user);


        }

    }

    public void genMerchant() {

        List<String> list = techMapper.list();

        String startDate = "2025-02-28";
        String endDate = "2025-03-31";

        Date start = DateUtil.toDate(startDate);
        Date end = DateUtil.toDate(endDate);

        // 生成20个按时间排序的随机时间
        List<String> sortedTimes = RandomGenerator.generateSortedRandomTimes(start, end, 80);

        for (int i = 0; i < list.size(); i++) {
            Merchant merchant = new Merchant();
            merchant.setName(list.get(i));
            merchant.setMobile(GenUtil.generateMobileNumber());
            merchant.setCity("杭州市");
            merchant.setProvince("浙江省");

            String createTime = sortedTimes.get(i);

            merchant.setCreateTime(createTime);

            String checkin = RandomGenerator.nextTime(createTime);

            merchant.setCheckTime(checkin);
            merchant.setUpdateTime(RandomGenerator.nextTime(checkin));

            merchantMapper.insert(merchant);
        }

//        for (String name : list) {
//
//            Merchant merchant = new Merchant();
//            merchant.setName(name);
//            merchant.setMobile(GenUtil.generateMobileNumber());
//            merchant.setCity("杭州市");
//            merchant.setProvince("浙江省");
//            merchantMapper.insert(merchant);
//
//        }

    }


    public static void main(String[] args) {


        LocalDate startDate = LocalDate.of(2025, 4, 1);

        System.out.println(startDate);


//        LocalDate localDate = LocalDate.now().plusDays(-54);
//        System.out.println(localDate);
//
//        String startDate = "2025-02-28";
//        String endDate = "2025-03-31";
//
//        Date date = DateUtil.toDate(startDate);
//        Date date2 = DateUtil.toDate(endDate);
//
//        System.out.println(date);
//
//        System.out.println(createMemberId("2025-04-08 11:13:10"));


    }

}
